<?php
/**
 * @author GhostWei
 * @date Sep 15, 2012
 */
abstract class oauth_api_response {
	
	public function __construct($data = null) {
		$this->setData($data);
	}

	protected $_data = array();
	/**
	 * @return oauth_api_response
	 */
	public function setData($data) {
		try {
			if(is_string($data)) {
				$this->_data = (array) json_decode($data);
			} elseif (is_object($data)) {
				$this->_data = (array) $data;
			} else {
				$this->_data = $data;
			}
		} catch (Exception $e) {
			$this->addError($e->getMessage());
		}
		return $this;
	}
	
	public function __call($method, array $args) {
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			$index = $matches[1];
			$default = isset($args[0]) ? $args[0] : null;
			return $this->get($index, $default);
		}
		return null;
	}
	
	public function get($index, $default = null) {
		return isset($this->_data[$index]) ? $this->_data[$index] : $default;
	}
	
	public function toArray() {
		return $this->_data;
	}
	
	private $_errors = array();
	/**
	 * @return oauth_api_response
	 */
	public function addError($msg, $code = null) {
		if($code === null) {
			$this->_errors[] = $msg;
		} else {
			$this->_errors[$code] = $msg;
		}
		return $this;
	}
	public function getError() {
		return $this->_errors;
	}
	
	protected function _pageSet() {
		$class = new stdClass();
		$class->total = 0;
		$class->page = 1;
		$class->pages = array();
		return $class;
	}
	
	protected function _rowTweet() {
		$class = new stdClass();
		$class->id = time() . rand(1000, 9999);
		$class->content = '';
		$class->quote = null;
		$class->author = $this->_rowAuthor();
		$class->images = array();
		$class->videos = array();
		$class->source = null;
		$class->hot = 0;
		$class->timeline = 0;
		return $class;
	}
	
	protected function _rowTopic() {
		$class = new stdClass();
		$class->name = '';
		$class->title = '';
		$class->description = '';
		$class->hot = 0;
		return $class;
	}
	
	protected function _rowAuthor() {
		$class = new stdClass();
		$class->id = '';
		$class->name = '';
		$class->location = '';
		$class->description = '';
		$class->homepage = '';
		$class->avatar = '';
		$class->gender = 0;
		$class->tweets = 0;
		$class->follows = 0;
		$class->following = 0;
		return $class;
	}
	
	abstract public function getNickname();
	abstract public function getAvatar();
	abstract public function getGender();
	abstract public function getUid();
	abstract public function getTopics();
	abstract public function getUser();
	abstract public function getTweets();
	abstract public function getTopicTweetsPaginator();
	abstract public function getUserTweetsPaginator();
}
